package test.org.example.java.nio;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;

/**
 * 编码测试
 * Java里使用的是UTF-16BE方式来存储数据的
 * Created by Juntao.Zhang on 2014/9/6.
 */
public class BufferToText {
  private static final int BSIZE = 1024;

  @Test
  public void simple() throws Exception {
    FileChannel fc = new FileOutputStream("data/charset/default.txt").getChannel();
    fc.write(ByteBuffer.wrap(
        "你好张峻滔\nhello Juntao.zhang".getBytes("UTF-8")));
    fc.close();

    fc = new FileInputStream("data/charset/default.txt").getChannel();
    fc.position(10);
    ByteBuffer buff = ByteBuffer.allocate(12);
    fc.read(buff);
    buff.flip();
    System.out.println(Charset.forName("UTF-8").decode(buff));
  }

  @Test
  public void generateDefault() throws Exception {
    FileChannel fc = new FileOutputStream("data/charset/default.txt").getChannel();
    fc.write(ByteBuffer.wrap(
        "你好Juntao.zhang".getBytes()));
    fc.close();
  }

  @Test
  public void readDefault() throws Exception {
    ByteBuffer buff = ByteBuffer.allocate(BSIZE);
    FileChannel fc = new FileInputStream("data/charset/default.txt").getChannel();
    buff.clear();
    fc.read(buff);
    buff.flip();
    System.out.println(Charset.forName(System.getProperty("file.encoding")).decode(buff));
//    System.out.println(buff.asCharBuffer());
  }

  @Test
  public void default2() throws Exception {
    ByteBuffer buff = ByteBuffer.allocate(BSIZE);
    // Use a CharBuffer to write through:
    FileChannel fc = new FileOutputStream("data/charset/default2.txt").getChannel();
    buff.asCharBuffer().put("你好Juntao.zhang");
    fc.write(buff);
    fc.close();
    // Read and display:
    fc = new FileInputStream("data/charset/default2.txt").getChannel();
    buff.clear();
    fc.read(buff);
    buff.flip();
    System.out.println(buff.asCharBuffer());
    buff.rewind();
    System.out.println(Charset.forName("UTF-16BE").decode(buff));

  }

  // UTF-8
  @Test
  public void generateUTF_8() throws Exception {
    FileChannel fc = new FileOutputStream("data/charset/utf-8.txt").getChannel();
    fc.write(ByteBuffer.wrap(
        "你好Juntao.zhang".getBytes("UTF-8")));
    fc.close();
  }

  @Test
  public void readUTF_8() throws Exception {
    ByteBuffer buff = ByteBuffer.allocate(10);
    FileChannel fc = new FileInputStream("data/charset/UTF-8.txt").getChannel();
    buff.clear();
    fc.read(buff);
    buff.flip();
    System.out.println(Charset.forName("UTF-8").decode(buff));
    buff.clear();
    fc.read(buff);
    buff.flip();
    System.out.println(Charset.forName("UTF-8").decode(buff));
//    System.out.println(buff.asCharBuffer());
  }

  // GBK
  @Test
  public void generateGBK() throws Exception {
    FileChannel fc = new FileOutputStream("data/charset/GBK.txt").getChannel();
    fc.write(ByteBuffer.wrap(
        "你好Juntao.zhang".getBytes("GBK")));
    fc.close();
  }

  @Test
  public void readGBK() throws Exception {
    ByteBuffer buff = ByteBuffer.allocate(BSIZE);
    FileChannel fc = new FileInputStream("data/charset/GBK.txt").getChannel();
    buff.clear();
    fc.read(buff);
    buff.flip();
    System.out.println(Charset.forName("GBK").decode(buff));
//    System.out.println(buff.asCharBuffer());
  }

  // UTF-16BE
  @Test
  public void generateUTF_16BE() throws Exception {
    FileChannel fc = new FileOutputStream("data/charset/UTF-16BE.txt").getChannel();
    fc.write(ByteBuffer.wrap(
        "你好Juntao.zhang".getBytes("UTF-16BE")));
    fc.close();
  }

  @Test
  public void readUTF_16BE() throws Exception {
    ByteBuffer buff = ByteBuffer.allocate(BSIZE);
    FileChannel fc = new FileInputStream("data/charset/UTF-16BE.txt").getChannel();
    buff.clear();
    fc.read(buff);
    buff.flip();
    System.out.println(buff.asCharBuffer());
  }

  // UTF-16LE
  @Test
  public void generateUTF_16LE() throws Exception {
    FileChannel fc = new FileOutputStream("data/charset/UTF-16LE.txt").getChannel();
    fc.write(ByteBuffer.wrap(
        "你好Juntao.zhang".getBytes("UTF-16LE")));
    fc.close();
  }

  @Test
  public void readUTF_16LE() throws Exception {
    ByteBuffer buff = ByteBuffer.allocate(BSIZE);
    FileChannel fc = new FileInputStream("data/charset/UTF-16LE.txt").getChannel();
    buff.clear();
    fc.read(buff);
    buff.flip();
    System.out.println(Charset.forName("UTF-16LE").decode(buff));
//    System.out.println(buff.asCharBuffer());
  }

  @Test
  public void readFile() throws Exception {
    Long start = System.currentTimeMillis();
    ByteBuffer buff = ByteBuffer.allocate(BSIZE);
    FileChannel fc = new FileInputStream("../data/ml-10m/ml-10M100K/ratings.dat").getChannel();
    while (fc.read(buff)!=-1) {
      buff.clear();
      fc.read(buff);
      buff.flip();
      System.out.println(Charset.forName("UTF-8").decode(buff));
    }
    System.out.println("cost :" + (System.currentTimeMillis() - start));
  }

}
